import asyncio
from dydx_v4_client.indexer.candles_resolution import CandlesResolution
from dydx_v4_client.indexer.rest.indexer_client import IndexerClient
from dydx_v4_client.network import TESTNET

# ------------ Markets ------------
MARKET_BTC_USD = "BTC-USD"


async def test():
    client = IndexerClient(TESTNET.rest_indexer)

    # Get perp market orderbook
    # try:
    #     response = await client.markets.get_perpetual_market_orderbook(
    #         market=MARKET_BTC_USD
    #     )
    #     print(response)
    #     print("orderbook")
    #     asks = response["asks"]
    #     bids = response["bids"]
    #     if asks:
    #         asks0 = asks[0]
    #         asks0_price = asks0["price"]
    #         asks0_size = asks0["size"]
    #         print(asks0_price)
    #         print(asks0_size)
    #     if bids:
    #         bids0 = bids[0]
    #         bids0_price = bids0["price"]
    #         bids0_size = bids0["size"]
    #         print(bids0_price)
    #         print(bids0_size)
    #     trades = response["trades"]
    #     print(trades)
    # except Exception as e:
    #     print(e)


    # Get perp market historical funding rates
    try:
        response = await client.markets.get_perpetual_market_historical_funding(
            market=MARKET_BTC_USD
        )
        print(response)
        print("historical funding")
        historical_funding = response["historicalFunding"]
        if historical_funding:
            historical_funding0 = historical_funding[0]
            print(historical_funding0)
    except Exception as e:
        print(e)


async def main():
    await test()


asyncio.run(main())